<template>
    <div>
<el-breadcrumb separator-class="el-icon-arrow-right">
  <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
  <el-breadcrumb-item>商品管理</el-breadcrumb-item>
  <el-breadcrumb-item>商品分类</el-breadcrumb-item>
</el-breadcrumb>

<!-- 卡片视图区域 -->
  <el-card>
     <el-row>
     <el-col>
     <el-button type="primary" @click="showaddcate">添加分类</el-button>
     </el-col>   
   </el-row>
   <!-- 表格 -->
<tree-table :data="catelist" :columns="columns" :selection-type="false"  :expand-type="false" show-index index-text="#" border class="fix">
  <!-- 是否有效 -->
  <template slot="isok" slot-scope="scope">
       <i class="el-icon-success" v-if="scope.row.cat_deleted===false" style="color:lightgreen;"></i>
        <i class="el-icon-error" v-else type="error" style="color:red;"></i>
  </template>
  <!-- 等级 -->
   <template slot="order" slot-scope="scope">
       <el-tag type="primary" v-if="scope.row.cat_level===0" >一级</el-tag>
          <el-tag type="success" v-else-if="scope.row.cat_level===1">二级</el-tag>
             <el-tag type="warning" v-else>三级</el-tag>
  </template>
<!-- 操作 -->
<template slot="opt" slot-scope="scope">
     <el-button type="primary" icon="el-icon-edit" @click="editthis(scope.row.cat_id)">编辑</el-button>
     <el-button type="danger" icon="el-icon-delete" @click="delcate(scope.row.cat_id)">删除</el-button>
  </template>

</tree-table>
   <!-- 分页区域 -->
     <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page=" queryinfo.pagenum"
      :page-sizes="[3, 5, 7,10]"
      :page-size=" queryinfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </el-card>
  
  <!-- 添加对话框 -->
  <el-dialog
  title="添加分类"
  :visible.sync="addcatedialogVisible"
  width="50%" @close="dateclose">
<el-form :model="addcateForm" :rules="addcaterules" ref="addcateruleForm" label-width="100px" class="demo-ruleForm">
  <el-form-item label="分类名称"  prop="cat_name">
    <el-input v-model="addcateForm.cat_name"></el-input>
  </el-form-item>
<!-- 
  父级分类 -->
  <el-form-item label="父级分类"  >
    <el-cascader
    v-model="selectkeys"
    :options="paraentcatelist"
    :props="{ expandTrigger: 'hover',value: 'cat_id',label: 'cat_name',children:'children' }"
    @change="parentcateChange" clearable change-on-select></el-cascader>
  </el-form-item>
  </el-form>
<!-- 登陆取消键： -->
  <span slot="footer" class="dialog-footer">
    <el-button @click="addcatedialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="addcate">确 定</el-button>
  </span>
</el-dialog>



  <!-- 编辑区域 -->
<el-dialog
  title="修改分类"
  :visible.sync="editdatelogVisible"
  width="50%">
  <el-form :model="editcateform" :rules="editrules" ref="editruleForm" label-width="100px" class="demo-ruleForm">
  <el-form-item label="分类名称" prop="cat_name">
    <el-input v-model="editcateform.cat_name"></el-input>
  </el-form-item></el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="editdatelogVisible = false">取 消</el-button>
    <el-button type="primary" @click="editsure">确 定</el-button>
  </span>
</el-dialog>
    </div>
</template>


<script>


export default {
  data(){
      return {
          //查询条件
          queryinfo:{
              type:3,
              pagenum:1,
              pagesize:5
          },
          catelist:[],
          addcatedialogVisible:false,
          addcateForm:{},
          editcateform:{},
          cat_pid:0,
          cat_level:0,
          editdatelogVisible:false,
          selectkeys:[],
          options:{
        
            },
          paraentcatelist:[],
          editrules:{required:true,message:"请输入修改名称",trigger:'blur'},
          //总数据条数
          total:0,
          columns:[{
            label:'分类名称',
            prop:'cat_name'
          },{label:"是否有效",type:'template',template:"isok"},
          {label:"排序",type:'template',template:"order"},
             {label:"操作",type:'template',template:"opt"}],
             addcaterules:{cat_name:[{required:true,message:"请输入分类名称",trigger:'blur'}]}
      }
  },
  created(){
      this.getcatelist()
  },
  methods:{
      //获取商品分类数据
    async  getcatelist(){
        
           const {data:res}= await  this.$http.get('categories',{params:this.queryinfo})
                      if(res.meta.status!=200)
                      {return this.$message.error("获取商品分类失败失败！");}
                      this.catelist=res.data.result
                      this.total=res.data.total
    },
    //监听pagesize
    handleSizeChange(newsize){
        this.queryinfo.pagesize=newsize
        this.getcatelist();
    },
    handleCurrentChange(newpage){
 this.queryinfo.pagenum=newpage
  this.getcatelist();
    },
    showaddcate(){
      //获取父级分类的方法
      this.getparentlist()
       this.addcatedialogVisible=true
    },
   async getparentlist(){
   const {data:res}= await  this.$http.get('categories',{params:{type:2}})
   if(res.meta.status!=200) {return this.$message.error("获取父节点列表失败")}
   console.log(res.data)
    this.paraentcatelist=res.data

    },
    parentcateChange(){
      console.log(this.selectkeys)
      if(this.selectkeys.length>0){
        this.addcateForm.cat_pid = this.selectkeys[this.selectkeys.length -1]
            this.addcateForm.cat_level=this.selectkeys.length
            return
      }
      else{ this.addcateForm.cat_pid=0;
      this.addcateForm.cat_level=0;

      }
    },
    addcate(){
          this.$refs.addcateruleForm.validate(async vaild=>{
                    if(!vaild){return;}
                   const {data:res}= await this.$http.post('categories',this.addcateForm)
                      if(res.meta.status!=201)
                      {return this.$message.error("添加分类失败！");}
                    else  this.$message.success("添加分类成功！")
                        this.getcatelist();
                        this.addcatedialogVisible=false   })
    },
    dateclose(){
      this.$refs.addcateruleForm.resetFields();
      this.selectkeys=[];
      this.addcateForm.cat_pid=0;
      this.addcateForm.cat_level=0;

    },
   async editthis(id){ 
      const {data:res}= await this.$http.get('categories/'+id)
                      if(res.meta.status!=200)
                      {return this.$message.error("获取分类失败！");}
                    this.editcateform=res.data;
                  this.editdatelogVisible=true 
                    },
    editsure(){
      this.$refs.editruleForm.validate(async vaild=>{
                    if(!vaild){return;}
                   const {data:res}= await this.$http.put('categories/'+this.editcateform.cat_id,
          {
            cat_name: this.editcateform.cat_name
          })
                      if(res.meta.status!=200)
                      {return this.$message.error("更新分类失败！");}
                    else  this.$message.success("更新分类成功！")
                        this.getcatelist();
                         this.editdatelogVisible=false   })
   
    },
   async  delcate(id){const confiresult =await  this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err=>err)
           if(confiresult!=='confirm') { return this.$message.info("已经取消删除！");}
               const {data:res} =await this.$http.delete('categories/'+id)
    if(res.meta.status!=200)
                  {  return this.$message.error("删除分类信息失败！");}
                this.$message.success("删除分类信息成功！")
              this.getcatelist();
}
  }
}
</script>

<style lang="less" scoped>
.fix{
  margin-top: 15px;
}
.el-cascader{
  width: 100%;
}
</style>
